package com.nanfang51g3.ckz.ui.registered;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.ui.base.IPresenter;
import com.nanfang51g3.ckz.ui.base.Base2Activity;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.ButtonUtils;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.widget.ClearEditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2016/4/13.
 */
public class Registered2Activity extends Base2Activity implements RegisteredContract.View {

    RegisteredContract.RegisteredActionsListener registeredActionsListener;
    @Bind(R.id.index_tv_right_title)
    TextView indexTvRightTitle;
    @Bind(R.id.bt_registered2_code)
    Button btRegistered2Code;
    @Bind(R.id.cet_registered2_code)
    ClearEditText cetRegistered2Code;
    String phone;
    private TimeCount time = new TimeCount(60 * 1000, 1000);
    @Override
    public IPresenter getPresenter() {
        return null;
    }
    @Override
    public int getContentView() {
        return R.layout.registered2;
    }

    @Override
    public boolean isShowLeftIcon() {
        return true;
    }

    @Override
    public boolean isShowRightIcon() {
        return false;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_accounting_left;
    }

    @Override
    public int setRightIcon() {
        return 0;
    }

    @Override
    public int showTitle() {
        return R.string.str_code;
    }

    @Override
    public void onClickLeft(View v) {
        String value = btRegistered2Code.getText().toString().trim();
        String va = getResources().getString(R.string.str_get_code);
        if (value.equals(va)) {
            AppManager.getAppManager().finishActivity(this);
        }
    }

    @Override
    public void onClickRight(View v) {
        startActivity(new Intent(this, Registered3Activity.class));
    }

    @Override
    public void showProgress() {
        super.showPro(R.string.str_progress_login);
    }

    @Override
    public void dismissProgress() {
        super.dismissPro();
    }

    @Override
    public void startShowView() {
        startActivity(new Intent(Registered2Activity.this, Registered3Activity.class).putExtra("phone", phone));
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showMessage(String message) {
        EasyToast.showShort(this, message);
    }

    /**
     * 倒计时
     *
     * @author Administrator
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {
            btRegistered2Code.setText(R.string.str_get_code);
            btRegistered2Code.setEnabled(true);// 可点击
            btRegistered2Code.setClickable(true);// 可点击
            btRegistered2Code.setPressed(false);// 未压下状态

        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            KLog.i("Evd", btRegistered2Code == null ? "111-" + millisUntilFinished : "000-" + millisUntilFinished);
            btRegistered2Code.setText(String.format(getResStr(R.string.str_timer),
                    millisUntilFinished / 1000));
            btRegistered2Code.setPressed(true);// 压下状态

        }
    }

    public String getResStr(int id) {
        String str = "";
        try {
            str = getResources().getString(id);
        } catch (Resources.NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            str = "";
        }
        return str;
    }

    @OnClick(R.id.bt_registered2_code)
    public void bt_registered2_code(View view) {
        btRegistered2Code.setEnabled(false);// 不可点击
        btRegistered2Code.setClickable(false);// 不可点击
        time.start();
        /**
         * @see  RegisteredPresenter#getRegisteredCode(String, boolean)
         */
        registeredActionsListener.getRegisteredCode(phone, false);
    }

    @Override
    public void onIntent() {
        super.onIntent();
        phone = getIntent().getStringExtra("phone");
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        registeredActionsListener = DaggerRegisteredContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .registeredPresenterModule(new RegisteredPresenterModule(this))
                .build().getRegisteredPresenter();
        Log.i("TAG", registeredActionsListener == null ? "registeredActionsListener==null" : "registeredActionsListener!=null");
        KLog.i("Evd", "onCreate");
        ButterKnife.bind(this);
        btRegistered2Code.setEnabled(false);// 不可点击
        btRegistered2Code.setClickable(false);// 不可点击
        time.start();
        indexTvRightTitle.setVisibility(View.VISIBLE);
        indexTvRightTitle.setText(getResources().getString(R.string.str_next));
        indexTvRightTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ButtonUtils.isFastDoubleClick(R.id.index_tv_right_title)) {
                    String code = cetRegistered2Code.getText().toString().trim();
                    /**
                     * @see RegisteredPresenter#verificationCode(String, String)
                     */
                    registeredActionsListener.verificationCode(code, phone);
                }

            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);//解除绑定，官方文档只对fragment做了解绑
        time.cancel();
        KLog.i("Evd", "onDestroy");
    }
}
